<template>
	<el-input v-show="isShow" size="small" v-model="value" :name="field.vModel" :clearable="clearable"
		:disabled="isDisabled" :placeholder="field.fieldRemark" class="input-with-select">
		<el-button id="selectId" v-if="!isReadOnly" slot="append" @click="openDialogClick()" type="primary"
			icon="el-icon-search"></el-button>
	</el-input>
</template>
<script>
	export default {
		name: "f2bpm-map-address",
		props: ['field', 'fieldvalue'],
		components: {},
		watch: {
			value(val, oleV) {
				var that = this;
				if (val != undefined && oleV != undefined && f2grid_global_mainVueLoadComplated) {
					that.data.addr = val;
					this.$emit('updatevalue', that.field, val);
				}
			},
			fieldvalue: {
				handler(newV, oleV) {
					var that = this;
					if (newV != undefined && oleV != undefined && f2grid_global_mainVueLoadComplated) {
						that.value = newV;
					}
				},
				immediate: false
			}
		},
		created: function() {
			var that = this;
			that.initailFieldOption();
			that.value = that.fieldvalue;
			if (that.value) {
				that.data.addr = that.value;
			}
			var formAction = WFContext.WorkflowformAction;
			that.isView = formAction > 2;
			if (that.isView) {
				that.isReadOnly = true;
			} else if (that.field._isReadOnly == true) {
				that.isReadOnly = true;
			}
		},
		mounted() {
			var that = this;
			//FUI.F2Grid.excuteFieldWritePowerByVueFormItem(that);
			if (that.isReadOnly == true) {
				that.isDisabled = true;
			}
		},
		data: function() {
			return {
				clearable:true,
				isDisabled: false,
				amapAccessKey: "",
				isView: false,
				isShow: true,
				isReadOnly: false,
				value: '',
				data: {
					addr: "",
					lon: "",
					lat: ""
				}
			}
		},
		methods: {
			initailFieldOption() {
				var that = this;
				if (that.field.fieldOptions && that.field.fieldOptions != "") {
					var fieldOptionsObj = typeof that.field.fieldOptions == "string" ? eval(that.field.fieldOptions) : that
						.field.fieldOptions;
					var general = fieldOptionsObj.find(a => a.opttype == 'general');
					if (general) {
						var kvData = general.params.kvData;
						var item = kvData.find(a => a.key == 'clearable');
						if (item) {
							that.clearable = item.value;
						}
						item = kvData.find(a => a.key == 'amapAccessKey');
						if (item) {
							that.amapAccessKey = item.value;
						}
					}
				}
			},
			openDialogClick() {
				var that = this;
				WF.SelectorDialog.getMapAddress(that, that.amapAccessKey, "addr", "lon", "lat", function(json) {
					that.data.address = json[0].addr;
					that.value = that.data.address;
				});
				return false;
			}
		}
	};
</script>
